package com.rul.mars.user.controller;

import com.rul.mars.common.api.CommonPage;
import com.rul.mars.common.api.CommonResult;
import com.rul.mars.mbg.domain.RoleParam;
import com.rul.mars.mbg.model.Resource;
import com.rul.mars.mbg.model.Role;
import com.rul.mars.user.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色服务接口
 *
 * @author RuL
 */
@Api(tags = "RoleController", description = "角色服务接口")
@RestController
@RequestMapping(path = "/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 创建角色
     *
     * @param param 创建角色所需参数
     * @return 创建结果
     */
    @ApiOperation("添加角色")
    @PostMapping(value = "/create")
    public CommonResult create(@RequestBody RoleParam param) {
        int count = roleService.create(param);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 修改角色信息
     *
     * @param role 角色信息
     * @return 修改结果
     */
    @ApiOperation("修改角色")
    @PostMapping(value = "/update")
    public CommonResult update(@RequestBody Role role) {
        int count = roleService.update(role);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 批量删除角色
     *
     * @param ids 角色ID
     * @return 删除结果
     */
    @ApiOperation("批量删除角色")
    @PostMapping(value = "/delete")
    public CommonResult<Integer> delete(@RequestParam("ids") List<Long> ids) {
        int count = roleService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 获取所有角色信息
     *
     * @return 所有角色信息
     */
    @ApiOperation("获取所有角色")
    @GetMapping(value = "/listAll")
    public CommonResult<List<Role>> listAll() {
        List<Role> roleList = roleService.list();
        return CommonResult.success(roleList);
    }

    /**
     * 分页获取角色信息
     *
     * @param pageSize 页面大小
     * @param pageNum  页号
     * @return 分页信息
     */
    @ApiOperation("分页获取角色列表")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<Role>> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<Role> roleList = roleService.list(pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(roleList));
    }

    /**
     * 修改角色状态
     *
     * @param id     角色ID
     * @param status 角色状态
     * @return 修改结果
     */
    @ApiOperation("修改角色状态")
    @PostMapping(value = "/updateStatus/{id}")
    public CommonResult updateStatus(@PathVariable Long id, @RequestParam(value = "status") Byte status) {
        Role role = new Role();
        role.setId(id);
        role.setStatus(status);
        int count = roleService.update(role);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 获取角色相关资源
     *
     * @param roleId 角色ID
     * @return 角色相关资源
     */
    @ApiOperation("获取角色相关资源")
    @GetMapping(value = "/listResource/{roleId}")
    public CommonResult<List<Resource>> listResource(@PathVariable Long roleId) {
        List<Resource> roleList = roleService.listResource(roleId);
        return CommonResult.success(roleList);
    }

    /**
     * 给角色分配资源
     *
     * @param roleId      角色ID
     * @param resourceIds 资源ID列表
     * @return 分配结果
     */
    @ApiOperation("给角色分配资源")
    @PostMapping(value = "/allocResource")
    public CommonResult allocResource(@RequestParam Long roleId, @RequestParam List<Long> resourceIds) {
        int count = roleService.allocResource(roleId, resourceIds);
        return CommonResult.success(count);
    }

}
